package com.richest.api.qiniu;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

//import org.apache.commons.io.IOUtils;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.richest.util.StringUtil;

/**
 * 七牛云存储
 */
public class QiniuCloudStorageService extends CloudStorageService {
	private UploadManager uploadManager;

	public BucketManager bucketManager;
	private String token;

	public QiniuCloudStorageService(CloudStorageConfig config) {
		this.config = config;

		// 初始化
		init();
	}

	private void init() {
		Configuration cfg = new Configuration(Zone.autoZone());
		uploadManager = new UploadManager(cfg);
		Auth auth = Auth.create(config.getQiniuAccessKey(), config.getQiniuSecretKey());
		token = auth.uploadToken(config.getQiniuBucketName());
		bucketManager = new BucketManager(auth, new Configuration(Zone.autoZone()));
	}

	/**
	 * @Title: deleteFile @Description: TODO(根据文件名删除文件) @param: @param
	 * fileName @return: void @throws
	 */
	@Override
	public void deleteFile(String fileName) {
		try {
			Response res = bucketManager.delete(config.getQiniuBucketName(), fileName);
			int status = res.statusCode;
			System.out.println(status);
		} catch (QiniuException e) {
			// TODO Auto-generated catch block
			int statusCode = e.response.statusCode;
			/*
			 * 待删除资源不存在
			 */
			System.out.println(statusCode);
			if (statusCode == 612) {

			}
		}
	}

	/**
	 * @Title: deleteByPath @Description: TODO(根据文件完整路径删除文件) @param: @param
	 * filePath @return: void @throws
	 */
	@Override
	public void deleteByPath(String filePath) {
		String domain = config.getQiniuDomain();
		String fileName = filePath.substring(domain.length() + 1);
		deleteFile(fileName);
	}

	@Override
	public String upload(byte[] data, String path) {
		try {
			Response res = uploadManager.put(data, path, token);
			if (!res.isOK()) {
				throw new RuntimeException("上传七牛出错：" + res.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				throw new Exception("上传文件失败，请核对七牛配置信息");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

		return config.getQiniuDomain() + "/" + path;
	}

	@Override
	public String upload(InputStream inputStream, String path) {
		try {
			byte[] data = IOUtils.toByteArray(inputStream);
			return this.upload(data, path);
		} catch (IOException e) {
			try {
				throw new Exception("上传文件失败", e);
			} catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}

	@Override
	public String uploadSuffix(byte[] data, String suffix) {
		return upload(data, getPath(config.getQiniuPrefix(), suffix));
	}

	@Override
	public String uploadPrefixSuffix(byte[] data, String prefix, String suffix) {
		prefix = config.getQiniuPrefix() + "/" + prefix;
		return upload(data, getPath(prefix, suffix));
	}

	@Override
	public String uploadFileName(String url) {
		if (StringUtil.isNullOrEmpty(url)) {
			return "";
		}
		String name[] = url.split("/");
		if (name != null && name.length > 0) {
			return name[name.length - 1];
		}
		return "";
	}

	@Override
	public String uploadSuffix(InputStream inputStream, String suffix) {
		return upload(inputStream, getPath(config.getQiniuPrefix(), suffix));
	}
}
